<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>选择</title>
	</head>
	<body>
		<p>这是一段文本</p>
		<button id="getText">获取选择的文本</button>

		<script>
			const getText = document.getElementById('getText')

			getText.addEventListener('click', function () {
				const selection = document.getSelection()
				alert(selection.getRangeAt(0))
			})
		</script>
	</body>
</html>
